import Reflux from 'reflux';
import Actions from "./Actions";
import update from 'react-addons-update';

import {Modal, Toast} from "antd-mobile";

import DB from '../../../../../app/db';
import StorageUtils from "../../../../../app/storage";

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    userInfo: {},
    nominations: [],
    currentNomination: {},
    jobs: [],
    cid: '',
    pid: '',
    isZr: false
  },

  onInit(cid, pid) {
    this.data.userInfo = update(this.data.userInfo, {$set: StorageUtils.getStorageItem('user')});
    this.data.cid = cid;
    this.data.pid = pid;
    this.updateComponent();
    this.hasRole(cid, pid, '主任');
    this.getWeiYuan(cid);
    this.getCurrentNomination(pid);
    this.getWyJobs();
    this.getWyJobById(cid, pid);
  },

  hasRole(cid, pid, roleName) {
    let t = this;
    DB.ywh.hasRole({
      courtId: cid,
      proprietorId: pid,
      roleName: roleName
    }).then(
      function (content) {
        t.data.isZr = content;
        t.updateComponent();
      },
      function (error) {
        console.log(error);
        t.data.isZr = false;
        t.updateComponent();
      }
    );
  },

  getCurrentNomination(id) {
    let t = this;
    DB.WechatAPI.getProprietorById({id: id}).then(
      function (content) {
        t.data.currentNomination = update(t.data.currentNomination, {$merge: content});
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      }
    );
  },

  getWeiYuan(courtId) {
    let t = this;
    DB.ywh.memberByRole({courtId: courtId, roleName: '委员'}).then(
      function (content) {
        let nominations = [];
        if (content && content.length > 0) {
          content.map(d => {
            nominations.push({
              value: d.proprietorId,
              label: d.proprietorName,
              ...d,
            });
          })
        }
        t.data.nominations = nominations;
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      }
    );
  },

  getWyJobs() {
    let t = this;
    DB.ywh.wyJobs().then(
      function (content) {
        let jobs = [];
        if (content && content.length > 0) {
          content.map(d => {
            jobs.push({value: d.id, label: d.roleName});
          });
        }
        t.data.jobs = jobs;
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      }
    );
  },

  getWyJobById(cid, pid) {
    let t = this;
    DB.ywh.wyJobById({courtId: cid, proprietorId: pid}).then(
      function (content) {
        if (content) {
          t.data.currentNomination = update(t.data.currentNomination, {
            job: {$set: content.roleId}
          });
          t.updateComponent();
        }
      },
      function (error) {
        console.log(error);
      }
    );
  },

  onJobChange(val) {
    this.data.currentNomination = update(this.data.currentNomination, {
      job: {$set: val[0]}
    });
    this.updateComponent();
  },

  onAppoint() {
    let t = this;
    let nomination = t.data.currentNomination;
    if (!(nomination.hasOwnProperty('job') && nomination.job)) {
      Toast.info('请选择职务');
      return;
    }
    if (!(nomination.hasOwnProperty('name') && nomination.name)) {
      Toast.info('请选择人员');
      return;
    }
    Modal.alert('提示', '确认要进行该项任命？', [
      {
        text: '确定',
        onPress: () => {
          let nomination = t.data.nominations.filter(d=> {
            return d.proprietorId === t.data.currentNomination.id;
          });
          DB.ywh.roleAppoint({
            courtId: t.data.cid,
            proprietorId: t.data.pid,
            doorPlateId: nomination[0].doorplateId,
            roleId: t.data.currentNomination.job
          }).then(
            function (content) {
              if (content) {
                Toast.success('操作成功！');
              } else {
                Toast.fail('操作失败！')
              }
            },
            function (error) {
              console.log(error);
            }
          );
        },
        style: 'default'
      },
      {
        text: '取消',
        onPress: () => {},
        style: 'default'
      }
    ]);
  },

  updateComponent() {
    this.trigger(this.data);
  },

  getInitialState() {
    return this.data;
  }
});
